<?php
//
//  VictoryCMS - Content managment system and framework.
//  Copyright (C) 2009  Lewis Gunsch
//
//  This file is part of VictoryCMS.
//  
//  VictoryCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  VictoryCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with VictoryCMS.  If not, see <http://www.gnu.org/licenses/>.

/**
 * VictoryCMS - HtmlList
 * 
 * @license http://www.gnu.org/licenses/gpl.html
 * @author Lewis Gunsch
 * @filesource
 * @package HtmlBuilders
 */

/**
 * This represents a list of HTML elements. This should be used
 * along with the other HTML building classes.
 * 
 * @package HtmlBuilders
 * 
 * @todo Create a PHPUnit test for this class
 */
class HtmlList {
	
	/** Array holding the HTML list. */
	protected $list;
	
	/** Current HTML list size. */
	protected $index;
	
	public function __construct() {
		$this->index = 0;
		$this->list = array();
	}
	
	public function __destruct() {
		unset($this->list);
		unset($this->index);
	}
	
	/**
	 * Returns the rendered HTML list in a string.
	 * 
	 * @return String The rendered form
	 */
	public function render() {
		$html = "";
		foreach ( $this->list as $formItem ) {
			$html .= $formItem;
		}
		return $html;
	}
	
	
	/**
	 * Return the HTML list rendered to an array of Strings.
	 * Each element of the HTML list is in a separate index.
	 * 
	 * @return Array The rendered html
	 */
	public function renderToArray() {
		// create a new array copy
		$rendered = array(); 
		foreach ( $this->list as $formItem ) {
			$html = $formItem . '\n';
			array_push( $rendered, $html );
		}
		return $rendered;
	}
	
	/**
	 * Adds an element onto the end of the HTML list.
	 * @param $html The html element to add.
	 */
	public function add($html) {
		$this->index++;
		array_push($this->list, $html);
	}
	
	/**
	 * Returns the current size of the HTML list.
	 * @return Integer The current size of the html list
	 */
	public function getSize() {
		return $this->index;
	}
}
?>